package com.appkudos.view;

import com.appkudos.controller.UserController;
import com.appkudos.model.App;
import com.appkudos.model.User;
import com.appkudos.view.R;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter.ViewBinder;

public class SelectAppActivity extends Activity implements ViewBinder{
    // list view for displaying the installed apps
	private ListView lv;
	private List<App> list;
	private PackageManager pm;
	private TextView title_tv;
	private Activity PARENTACTIVITY;
	private boolean[] selected;
	private List final_selection;
	UserController uc;
	User user;
	App app;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PARENTACTIVITY = this;
        final_selection = new ArrayList<ListItem>();
        // set content view to apps_view xml
        setContentView(R.layout.view_title_list);      
        
        // get data from bundle
        Intent launchIntent = getIntent();
        Bundle extras = launchIntent.getExtras();
        uc = new UserController();
        user = uc.getCurrentUser(extras);

        
        // set the title of the page 
        title_tv=(TextView)findViewById(R.id.tl_title);
        title_tv.setText("Select an app to recommend:");
      
        // get list view from layout
        lv=(ListView)findViewById(R.id.tl_list);
        
        // get installed apps from the device
//        pm = this.getPackageManager();
//        Intent intent = new Intent(Intent.ACTION_MAIN, null);
//        intent.addCategory(Intent.CATEGORY_LAUNCHER);
//        List apps = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);  
//        Iterator i = apps.iterator();
        
	    // initialize the list
        initInstalledApps();
		
		// when item is clicked go to app page
	    lv.setOnItemClickListener(new OnItemClickListener() {
	           public void onItemClick(AdapterView<?> parent, View view,
	               int position, long id) {
	        	   
	        	   // When clicked, show a toast with the TextView text
	        	   App item = (App) list.get(position);
	        	   
	        	   Intent intent = new Intent(PARENTACTIVITY, WriteReviewActivity.class);
	        	   intent = uc.passCurrentUser(intent, user);
	        	   intent.putExtra("app_name", item.getAppName());
	          	   intent.putExtra("app_desc", item.getAppDesc());
	          	   intent.putExtra("app_id", item.getAppid());
	          	   startActivity(intent);
	             
	           }
	    });
	       
		// set filter on listview
		lv.setTextFilterEnabled(true);
   }
    
    /**
     * populate the list with installed apps
     * @param pm - package manager
     * @param i - iterator
     */
     private void initInstalledApps(){
    	Map<String, App> itemData;
    	List<Map<String, App>> listData = new ArrayList<Map<String, App>>();
    	
    	list = uc.getAppsForUser(user.getUsername());
    	
    	// put the apps into a hashmap (used for input by simple adapter)
    	for(int j=0; j<list.size();j++){
    		itemData = new HashMap<String, App>(1);
    		app = (App) list.get(j);
    		itemData.put("app_list_linearLayout1", app);
    		listData.add(itemData);
    	}    

    	// bind simple adapter to the list view
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, listData, R.layout.app_list,
                new String[] {
                    "app_list_linearLayout1"
                }, new int[] {
                    R.id.app_list_linearLayout1
                });
        
        // bind viewBinder to adapter and set adapter to listview
        simpleAdapter.setViewBinder(this);
        lv.setAdapter(simpleAdapter);
    }
    
    @Override
    public boolean setViewValue(View view, Object data, String stringRepresetation) {  	
    	// get data for the row
    	App app = (App) data;
        LinearLayout layout = (LinearLayout) view;
        
        // bind information to the textbox and icon
        TextView tv1 = (TextView) layout.findViewById(R.id.app_text);
        tv1.setText(app.getAppName());
        tv1.setCompoundDrawablesWithIntrinsicBounds(app.getIcon(), null, null, null);   
        
     // set the checkbox (checked if flagged as fav)
        CheckBox box = (CheckBox) layout.findViewById(R.id.fav_check);
        box.setVisibility(View.GONE);
        
        return true;
    }

    // private container class for keeping the app data
    private class ListItem implements Comparable<ListItem>{
        public String text;
        public Drawable icon;
        public Boolean check;
        public int pos;
        
		@Override
		public int compareTo(ListItem item) {
	        return text.compareTo(item.text);
	    }
    }
}